﻿using System;
using System.Collections.Generic;
using System.Text;

namespace FSUtility
{
    /// <summary>
    /// String和ASCII相互转换
    /// 可以作为简单的密码转化（仅在不严格的地方）
    /// </summary>
    public class TransformStrASCII
    {
        /// <summary>
        /// string转换为ASCII
        /// </summary>
        /// <param name="transformedStr">待转换的string</param>
        /// <returns>转化为ASCII的string值</returns>
        public string StringToASCII(string transformedStr)
        {
            if (string.IsNullOrEmpty(transformedStr))
            {
                return null;
            }

            byte[] by = Encoding.ASCII.GetBytes(transformedStr);

            return Convert.ToBase64String(by);
        }

        /// <summary>
        /// ASCII转化为string
        /// </summary>
        /// <param name="transformedASCII">待转换的ASCII值</param>
        /// <returns>转为string的ASCII码</returns>
        public string ASCIIToString(string transformedASCII)
        {
            if (string.IsNullOrEmpty(transformedASCII))
            {
                return null;
            }

            byte[] by = Convert.FromBase64String(transformedASCII);
            return Encoding.ASCII.GetString(by);
        }
    }
}
